package org.deltaset.meta.faces;

import java.util.List;
import java.util.Map;
import org.deltaset.meta.Finder;
import org.deltaset.meta.MetaActionHandler;
import org.deltaset.meta.MetaEntry;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author dmakariev
 */
class LazyTableModel extends LazyDataModel<Object> {

    private final Finder finder;
    private final MetaEntry metaEntry;
    private final MetaActionHandler metaActionHandler;

    LazyTableModel(Finder finder, MetaActionHandler metaActionHandler, MetaEntry metaEntry) {
        this.finder = finder;
        this.metaActionHandler = metaActionHandler;
        this.metaEntry = metaEntry;
    }

    @Override
    public Object getRowData(String rowKey) {
        return metaActionHandler.loadModel(metaEntry, rowKey);       
    }

    @Override
    public Object getRowKey(Object item) {
        return metaActionHandler.getModelId(item);
    }

    @Override
    public void setRowIndex(int rowIndex) {
        try {
            super.setRowIndex(rowIndex);
        } catch (ArithmeticException ex) {
            //primefaces are briliant ... as allways.. trying to devide by variable which could be 0...
            super.setPageSize(1);
            super.setRowIndex(rowIndex);
            super.setPageSize(0);
            
        }
    }

    @Override
    public List<Object> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {

        final int firstResult = first;
        final int maxResult = (0 == first) ? pageSize : first + first * pageSize;
        final List<Object> data = finder.finder(firstResult, maxResult);

//        final long counter = finder.counter();
//        final int rowCount = (counter < 0) ? data.size() : (int) counter;
//        this.setRowCount(rowCount);
        return data;

//        List<Object> data = new ArrayList<Object>();  
//  
//        //filter  
//        for(Car car : datasource) {  
//            boolean match = true;  
//  
//            for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {  
//                try {  
//                    String filterProperty = it.next();  
//                    String filterValue = filters.get(filterProperty);  
//                    String fieldValue = String.valueOf(car.getClass().getField(filterProperty).get(car));  
//  
//                    if(filterValue == null || fieldValue.startsWith(filterValue)) {  
//                        match = true;  
//                    }  
//                    else {  
//                        match = false;  
//                        break;  
//                    }  
//                } catch(Exception e) {  
//                    match = false;  
//                }   
//            }  
//  
//            if(match) {  
//                data.add(car);  
//            }  
//        }  
//  
//        //sort  
//        if(sortField != null) {  
//            Collections.sort(data, new LazySorter(sortField, sortOrder));  
//        }  
//  
//        //rowCount  
//        int dataSize = data.size();  
//        this.setRowCount(dataSize);  
//  
//        //paginate  
//        if(dataSize > pageSize) {  
//            try {  
//                return data.subList(first, first + pageSize);  
//            }  
//            catch(IndexOutOfBoundsException e) {  
//                return data.subList(first, first + (dataSize % pageSize));  
//            }  
//        }  
//        else {  
//            return data;  
//        }  
        // throw new UnsupportedOperationException("not yet supported");
    }
}
